import requests from '@/api/request'
import { getToken } from '@/utils/token.js'

export const reqLogin = ({ mobile, code }) => {
  return requests({
    url: '/authorizations',
    method: 'post',
    data: {
      mobile,
      code
    }
  })
}

export const reqRefreshToken = () => {
  return requests({
    url: '/authorizations',
    method: 'PUT',
    headers: {
      Authorization: 'Bearer ' + localStorage.getItem('refresh_token')
    }
  })
}

export const reqAllChannels = () => {
  return requests({
    url: '/channels',
    method: 'get'
  })
}

export const reqUpdateChannels = (newChannel) => {
  return requests({
    url: '/user/channels',
    method: 'put',
    data: {
      channels: newChannel
    }
  })
}

export const reqUserChannels = () => {
  return requests({
    url: '/user/channels',
    method: 'get',
  })
}

export const reqArticleList = ({ channel_id, timestamp }) => {
  return requests({
    url: '/articles',
    method: 'get',
    params: {
      channel_id,
      timestamp
    }

  })
}

export const dislikeArticle = (artId) => {
  return requests({
    url: '/article/dislikes',
    method: 'post',
    data: {
      target: artId
    }

  })
}

export const reqsuggestList = ({ keyword }) => {
  return requests({
    url: '/suggestion',
    method: 'get',
    params: {
      q: keyword
    }
  })
}

export const reqSearchResult = ({ page = 1, q }) => {
  return requests({
    url: '/search',
    method: 'GET',
    params: {
      page,
      q
    }
  })
}

export const reportsArticle = ({ artId, type }) => {
  return requests({
    url: '/article/reports',
    method: 'post',
    data: {
      target: artId,
      type,
      remark: '嘿嘿'
    }

  })
}

export const reqArticleDetail = ({ art_id }) => {
  return requests({
    url: `/articles/${art_id}`,
    method: 'get',
  })
}

export const reqFollerAuthor = ({ user_id }) => {
  return requests({
    url: '/user/followings',
    method: 'POST',
    data: {
      target: user_id
    }
  })
}

export const requnFollerAuthor = ({ user_id }) => {
  return requests({
    url: `/user/followings/${user_id}`,
    method: 'DELETE',
  })
}

export const reqUserLike = ({ art_id }) => {
  return requests({
    url: '/article/likings',
    method: 'POST',
    data: {
      target: art_id
    }
  })
}

export const reqUserDislike = ({ art_id }) => {
  return requests({
    url: `/article/likings/${art_id}`,
    method: 'DELETE',
  })
}

export const reqCommentList = ({ art_id, offset = null, limit = 10 }) => {
  return requests({
    url: '/comments',
    method: 'GET',
    params: {
      type: 'a',
      source: art_id,
      offset,
      limit
    }
  })
}

export const reqCommentLike = ({ com_id }) => {
  return requests({
    url: '/comment/likings',
    method: 'POST',
    data: {
      target: com_id
    }
  })
}

export const reqCommentDislike = ({ com_id }) => {
  return requests({
    url: `/comment/likings/${com_id}`,
    method: 'DELETE',
  })
}

export const reqSendComment = ({ art_id, context }) => {
  return requests({
    url: '/comments',
    method: 'POST',
    data: {
      target: art_id,
      content: context
    }
  })
}

export const reqUserInfo = () => {
  return requests({
    url: '/user',
    method: 'get'
  })
}

export const reqUserProfile = () => {
  return requests({
    url: '/user/profile',
    method: 'get'
  })
}

export const reqUpdateUserPhoto = (fd) => {
  return requests({
    url: '/user/photo',
    method: 'PATCH',
    data: fd
  })
}

export const reqUpdateUserInfo = ({ name = '', birthday = '' }) => {
  return requests({
    url: '/user/profile',
    method: 'PATCH',
    data: {
      name,
      birthday
    }
  })
}